---
const { version } = Astro.props;
---

<style lang="scss">
  @use '../../public/styles/var' as *;

  .nav {
    position: sticky;
    top: 0;
    z-index: map-get($map: $layers, $key: 'nav') + 1;
    display: grid;
    grid-template-areas:
      'mobile logo version'
      'search search search';
    grid-template-rows: $nav-height $nav-height;
    grid-template-columns: 1fr 2fr 1fr;
    align-items: center;
    height: $nav-height;
    padding-right: 0.5rem;
    padding-left: 0.625rem;
    color: $white;
    background-color: $dark-blue;

    @media (min-width: $breakpoint-m) {
      display: flex;
      height: $nav-height;
      padding-right: 0;
    }
  }

  .hamburger {
    width: 1em;
    height: 1em;
    fill: currentColor;
  }

  .link {
    display: inline-block;
    padding: 0.5em;
    color: $white;
    font-weight: 500;
    text-decoration: none;
    opacity: 0.7;
    transition: opacity 150ms linear;

    &:focus,
    &:hover {
      opacity: 1;
    }

    &__desktop {
      display: none;

      @media (min-width: $breakpoint-m) {
        display: block;
      }
    }
  }

  .logo {
    display: flex;
    align-items: center;
    grid-area: logo;
    justify-content: center;
    padding: 0.5rem;
    color: $white;
    font-size: 24px;
    text-decoration: none;

    @media (min-width: $breakpoint-m) {
      justify-content: flex-start;
      padding: 0.5rem 1.25rem;
    }
  }

  .logo-icon {
    display: block;
    width: 1em;
    height: 1em;
    margin-right: 0.25em;
    margin-bottom: 0.15rem;
    fill: currentColor;
  }

  .logo-type {
    font-weight: 700;
    font-family: $heading;
    letter-spacing: -0.03em;
    padding-top: 0.2rem;
  }

  .mobile-open {
    display: flex;
    grid-area: mobile;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    padding: 0;
    color: $white;
    font-size: 16px;
    background: none;
    border: none;
    appearance: none;

    @media (min-width: $breakpoint-m) {
      display: none;
    }
  }

  .search {
    display: none;
    background: $dark-blue;
    position: relative;
    z-index: 1000;
    flex-grow: 1;
    grid-area: search;
    max-width: calc(100vw - 1.25rem);
    padding-left: 0.75rem;
    padding-right: 0.7rem;

    > * {
      flex-grow: 1;
    }

    > :global(.algolia-autocomplete) {
      width: 100%;
    }

    :global(body.is-nav-open) & {
      display: flex;
    }

    @media (min-width: $breakpoint-m) {
      display: flex;
      width: 600px;
      max-width: 600px;
      margin-right: auto;
    }
  }

  .sr-only {
    position: absolute;
    right: 1000px;
  }

  .search-hint {
    position: absolute;
    top: 0;
    right: 0.75rem;
    display: none;
    height: 100%;
    width: 3.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    letter-spacing: 0.125em;
    color: #c2ced9;
    font-size: 16px;
    pointer-events: none;

    & * {
      font-family: $code;
    }

    &::before {
      display: inline-block;
      width: 1px;
      height: 1.5em;
      margin-right: 0.625em;
      vertical-align: -35%;
      background-color: rgba($white, 0.25);
      content: '';
    }

    @media (min-width: $breakpoint-m) {
      display: flex;
    }
  }

  .search-input {
    flex-grow: 1;
    box-sizing: border-box;
    width: 100%;
    margin: 0;
    padding-top: 0.5rem;
    padding-right: 1rem;
    padding-bottom: 0.5rem;
    padding-left: 1rem;
    overflow: visible;
    color: #fff;
    font-weight: 500;
    font-size: 100%;
    font-family: inherit;
    line-height: inherit;
    background-color: #446e8f;
    border-color: rgba($white, 0.25);
    border-style: solid;
    border-width: 1px;
    border-radius: 0.25rem;
    outline: 0;
    transition-timing-function: ease-in-out;
    transition-duration: 0.2s;
    transition-property: border-color, color;
    -webkit-font-smoothing: antialiased;

    &:focus {
      color: white;
      border-color: rgba($white, 0.75);

      &::placeholder {
        color: rgba($white, 0.8);
      }
    }
    &::placeholder {
      color: #c2ced9;
    }

    &__desktop {
      display: none;

      @media (min-width: $breakpoint-m) {
        display: block;
      }
    }
  }

  .version {
    grid-area: version;
    margin-left: 0.5em;
    font-size: 0.8em;
    font-family: $code;
    text-align: right;

    @media (min-width: $breakpoint-m) {
      text-align: left;

      &::after {
        display: inline-block;
        width: 1px;
        height: 1.5em;
        margin-left: 1em;
        vertical-align: -25%;
        background-color: rgba($white, 0.25);
        content: '';
      }
    }
  }

  .social {
    width: 1em;
    height: 1em;
    fill: currentColor;
  }
</style>

<nav class="nav">
  <button id="toc-drawer-button" class="mobile-open" type="button" aria-expanded="false"
    aria-controls="nav-primary">
    <svg focusable="false" class="hamburger" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 448 512">
      <title>Toggle mobile menu</title>
      <path
        d="M16 132h416c8.837 0 16-7.163 16-16V76c0-8.837-7.163-16-16-16H16C7.163 60 0 67.163 0 76v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16z">
      </path>
    </svg>
  </button>
  <a class="logo" href="/">
    <svg class="logo-icon" viewbox="0 0 640 512" version="1.1" xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink">
      <g transform="translate(-1.000000, 0.000000)" fill-rule="nonzero">
        <path
          d="M635.92,462.7 L347.92,14.7 C342.03,5.54 331.89,0 321,0 C310.11,0 299.97,5.54 294.08,14.7 L6.08,462.7 C-0.250773249,472.547007 -0.699487627,485.064987 4.91,495.34 C10.522069,505.612419 21.2945349,512 33,512 L609,512 C620.71,512 631.48,505.61 637.09,495.33 C642.699457,485.058495 642.250708,472.543372 635.92,462.7 Z M321,91.18 L406.39,224 L321,224 L257,288 L218.94,249.94 L321,91.18 Z"
          id="Shape"></path>
      </g>
    </svg>
    <span class="logo-type">Snowpack</span>
  </a>
  <div class="search">
    <input type="text" name="search" placeholder="Search documentation..." class="search-input"
      id="search-form-input">
    <span class="search-hint">
      <span class="sr-only">Press </span>
      <kbd class="font-sans"><abbr title="Command" style="text-decoration: none;">⌘</abbr></kbd>
      <span class="sr-only"> and </span>
      <kbd class="font-sans">K</kbd>
      <span class="sr-only"> to search</span>
    </span>
  </div>

  <a href="https://github.com/FredKSchott/snowpack/releases" target="_blank" class="link version">
    {`v${version}`}
  </a>
  <a href="https://github.com/FredKSchott/snowpack" target="_blank" class="link link__desktop">
    <svg aria-hidden="true" focusable="false" data-prefix="fab" data-icon="github" class="social" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 496 512"><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg>
  </a>
</nav>

<script>
  function handleMobileNav(evt) {
    evt.preventDefault();
    /*If hidden-mobile class is enabled that means we are on desktop do overflow normal but we
      if we are at mobile fixed body position, so that its not scrollable(which currently causing bug) and navbar  handling its
      owns scroll. Case to consider there are chance use can open navbar using toggle button and user when click on any link
      body postion should be unset
      */
    document.body.classList.toggle('is-nav-open');
    const isOpen = document.body.classList.contains('is-nav-open');
    if (isOpen) {
      evt.target.setAttribute('aria-expanded', 'true');
    } else {
      evt.target.setAttribute('aria-expanded', 'false');
    }
  }

  const mobileNavBtn = document.getElementById('toc-drawer-button');
  mobileNavBtn.addEventListener('click', handleMobileNav);
  mobileNavBtn.addEventListener('touchend', handleMobileNav);
  if (window.location.pathname.startsWith('/posts')) {
    mobileNavBtn.style.display = 'none';
  }

  const searchFormInputEl = document.getElementById('search-form-input');
  searchFormInputEl.addEventListener('keyup', () => {
    const gridTocEl = document.querySelector('#nav-primary');
    if (searchFormInputEl.value) {
      gridTocEl.classList.add('is-mobile-hidden');
    } else {
      gridTocEl.classList.remove('is-mobile-hidden');
    }
  });

  document.onkeydown = function (e) {
    if ((e.ctrlKey || e.metaKey) && e.which == 75) {
      e.preventDefault();
      searchFormInputEl.focus();
    }
  };
</script>
<script type="module" src="./docsearch.js"></script>
<doc-search api-key="562139304880b94536fc53f5d65c5c19" selector="#search-form-input"></doc-search>
